import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.List;

import static java.lang.Thread.sleep;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        //Test01();
        //Test02();
        Test03();
    }

    private static void Test03() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        // 找到搜索框
        WebElement search_input = webDriver.findElement(By.cssSelector("#kw"));
        // 输入内容
        search_input.sendKeys("软件测试");
        // 清空搜索框中内容
        search_input.clear();
        // 输入新内容
        search_input.sendKeys("前端");
        // 找到按钮点击
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        // 校验
        List<WebElement> search_results = webDriver.findElements(By.cssSelector("a em"));
        for (WebElement x : search_results) {
            if (x.getText().equals("前端")) {
                System.out.println("测试通过");
            } else {
                System.out.println("测试不通过");
            }
        }
    }

    private static void Test02() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        // 找到搜索框
        WebElement search_input = webDriver.findElement(By.cssSelector("#kw"));
        // 输入内容
        search_input.sendKeys("软件测试");
        // 找到搜索按钮 (百度一下)
        WebElement baidu_button = webDriver.findElement(By.cssSelector("#su"));
        // 点击按钮
        baidu_button.click();
        sleep(3000);
        // 校验
        // 找到了页面上所有关于软件测试相关的元素
        List<WebElement> search_results = webDriver.findElements(By.cssSelector("a em"));
        for (WebElement x : search_results) {
            if (x.getText().equals("软件测试")) {
                System.out.println("测试通过");
            } else {
                System.out.println("测试不通过");
            }
        }
    }

    private static void Test01() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        //通过css选择器
        //WebElement search_input = webDriver.findElement(By.cssSelector(".s_ipt"));
        //通过xpath
        //WebElement search_input = webDriver.findElement(By.xpath("//form/span[1]/input"));
        //通过标签
        WebElement search_input = webDriver.findElement(By.tagName("input"));
        if (search_input == null) {
            System.out.println("测试不通过");
        } else {
            System.out.println("测试通过");
        }
        webDriver.quit();
    }
}